package com.xuecheng.order.controller;

import com.github.binarywang.wxpay.bean.notify.WxPayNotifyResponse;
import com.xuecheng.api.order.OrderApi;
import com.xuecheng.api.order.model.dto.OrdersDTO;
import com.xuecheng.api.order.model.pay.PayCodeUrlResult;
import com.xuecheng.api.order.model.qo.QueryOrdersModel;
import com.xuecheng.common.domain.code.CommonErrorCode;
import com.xuecheng.common.domain.page.PageRequestParams;
import com.xuecheng.common.domain.page.PageVO;
import com.xuecheng.common.domain.uaa.LoginUser;
import com.xuecheng.common.exception.ExceptionCast;
import com.xuecheng.order.common.utils.UAASecurityUtil;
import com.xuecheng.order.service.OrdersService;
import com.xuecheng.order.service.PayService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.io.IOUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

/**
 * <p>
 * 订单 前端控制器
 * </p>
 *
 * @author itcast
 */
@Slf4j
@RestController
public class OrdersController implements OrderApi {

    @Autowired
    private OrdersService  ordersService;

    @Autowired
    private PayService payService;

    @Override
    @PostMapping("orders/my-list")
    public PageVO<OrdersDTO> queryMyOrderList(PageRequestParams params,@RequestBody QueryOrdersModel model) {
        return null;
    }

    @Override
    @PostMapping("orders/org-list")
    public PageVO<OrdersDTO> queryMyOrgList(PageRequestParams params,@RequestBody QueryOrdersModel model) {
        return null;
    }

    @GetMapping("orders/create/{coursePubId}")
    public OrdersDTO createOrder(@PathVariable Long coursePubId) {

        LoginUser user = UAASecurityUtil.getUser();

        if (ObjectUtils.isEmpty(user)) {
            ExceptionCast.cast(CommonErrorCode.E_100108);
        }

        OrdersDTO dto = ordersService.createOrder(coursePubId, user.getUsername());


        return dto;
    }

    @GetMapping("orderPay/wxPay/createPay")
    public PayCodeUrlResult createPayment(String orderNo) {

        LoginUser user = UAASecurityUtil.getUser();

        if (ObjectUtils.isEmpty(user)) {
            ExceptionCast.cast(CommonErrorCode.E_100108);
        }

        PayCodeUrlResult payment = ordersService.createPayment(orderNo, user.getUsername());

        return payment;
    }

    /* 给wx支付平台来调用的方法，没有在Api接口中定义方法 */
    @PostMapping("order-pay/wx-pay/notify-result")
    public String notifyPayment(HttpServletRequest request) {
        System.out.println("订单支付通知方法开始执行");
        try {
            String xmlResult = IOUtils.toString(request.getInputStream(), request.getCharacterEncoding());

            // 加入自己处理订单的业务逻辑，需要判断订单是否已经支付过，否则可能会重复调用
            // 调用service层处理业务
            payService.notifyPayment(xmlResult);

            return WxPayNotifyResponse.success("处理成功!");
        } catch (Exception e) {
            log.error("微信回调结果异常,异常原因{}", e.getMessage());
            return WxPayNotifyResponse.fail(e.getMessage());
        }
    }

    //根据订单id查询



}
